# Maintainer: Felix Yan <felixonmars@archlinux.org>

pkgname=n2n
pkgver=3.1.1
pkgrel=2
pkgdesc="A Peer-to-peer VPN software which makes it easy to create virtual networks bypassing intermediate firewalls"
arch=('loong64' 'x86_64')
url="https://github.com/ntop/n2n"
license=('GPL')
depends=('libcap' 'openssl' 'miniupnpc' 'zstd')
optdepends=('libpcap: for n2n-decode')
makedepends=('libpcap')
backup=('etc/n2n/edge.conf'
        'etc/n2n/supernode.conf')
source=("https://github.com/ntop/n2n/archive/$pkgver/n2n-$pkgver.tar.gz")
sha512sums=('ba4ffbc758c399758e35b47308a663f33d04ecb9595f296fd77b13631cea08e04a7cd7369fe82e09149a31e145cd25610dd0a387853ec149e479df866b778cb6')

prepare() {
  cd $pkgname-$pkgver
  sed -i 's|/sbin|/bin|' Makefile.in
  sed -i 's|/usr/sbin|/usr/bin|' packages/etc/systemd/system/*.service.in

  # We need a better way to make the conf file owned by the user created in sysusers.d...
  sed -i '/User=n2n/d;/Group=n2n/d' packages/etc/systemd/system/supernode.service.in
}

build() {
  cd $pkgname-$pkgver
  ./autogen.sh
  ./configure --with-zstd --with-openssl --enable-miniupnp --enable-pcap --enable-cap --enable-pthread
  make
}

package() {
  cd $pkgname-$pkgver
  DESTDIR="$pkgdir" SBINDIR="$pkgdir"/usr/bin make install

  install -Dm600 packages/etc/n2n/edge.conf.sample "$pkgdir"/etc/n2n/edge.conf
  install -Dm600 packages/etc/n2n/supernode.conf.sample "$pkgdir"/etc/n2n/supernode.conf
  install -Dm644 packages/etc/systemd/system/edge.service.in "$pkgdir"/usr/lib/systemd/system/edge.service
  install -Dm644 packages/etc/systemd/system/edge@.service.in "$pkgdir"/usr/lib/systemd/system/edge@.service
  install -Dm644 packages/etc/systemd/system/supernode.service.in "$pkgdir"/usr/lib/systemd/system/supernode.service
}
